package com.lsa.southwind.controller;


import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.service.DormitoryService;
import com.lsa.southwind.util.ResultVoUtil;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private DormitoryService dormitoryService;

    @GetMapping("/availableList")
    public ResultVo availableList() {
        ResultVo availableList = this.dormitoryService.availableList();
        return availableList;
    }

    @PostMapping("/save")
    /*除了get不用其他都要加*/
    public ResultVo save(@RequestBody Dormitory dormitory) {
        dormitory.setAvailable(dormitory.getType());
        boolean save = this.dormitoryService.save(dormitory);
        if (!save) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVo listDormitory(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageVo list = this.dormitoryService.list(page, size);
        return ResultVoUtil.success(list);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm) {
        PageVo search = this.dormitoryService.search(searchForm);

        return ResultVoUtil.success(search);
    }

    /*学生修改*/
    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {
        Dormitory dormitory = this.dormitoryService.getById(id);
        return ResultVoUtil.success(dormitory);
    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody Dormitory dormitory) {
        boolean updateById = this.dormitoryService.updateById(dormitory);
        if (!updateById) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id) {
        Boolean deleteById = this.dormitoryService.deleteById(id);
        if (!deleteById) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }
}

